﻿Public Class businessVentas
    Public Function ConsultaVentas(ByVal Vta_Id As Int32, ByVal Cli_Id As Int32, ByVal ImporteDesde As Decimal, ByVal ImporteHasta As Decimal, ByVal FecDesde As Date, ByVal FecHasta As Date, ByVal Cod_Suc As Int32) As List(Of clsVentas)
        Dim ListaVentas As New List(Of clsVentas)
        Dim VentasDao As New daoVenta()
        Dim Cn As New SqlClient.SqlConnection
        Dim Cm As New SqlClient.SqlCommand
        Dim Dr As SqlClient.SqlDataReader
        Dim ListaOrden_Servicio As New List(Of clsOrden_Servicio)
        Dim StringSelect As String
        Dim StrinWhere As String

        Try
            ''si ingreso Nro OS bsuco por numero
            Dim Venta As New clsVentas()
            If Vta_Id > 0 Then
                Venta = VentasDao.TraeVenta(Vta_Id)
            End If
            If Venta.Vta_Id > 0 Then
                ListaVentas.Add(Venta)
                Return ListaVentas
            End If

            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.Text

            StringSelect = " Select vta_id, vta_fecha, cli_id, presu_id, " & _
                            " emp_id, vta_importe, Cod_Suc " & _
                            " from ventas "

            StrinWhere = " where Cod_Suc = " & Cod_Suc.ToString.Trim & " and " & _
                           " vta_fecha between '" & FecDesde.ToString & "' and '" & _
                           FecHasta.ToString & "'"

            If ImporteDesde > 0 And ImporteHasta > 0 Then
                StrinWhere = StrinWhere & " and vta_importe between " & ImporteDesde.ToString.Trim & _
                            " and " & ImporteHasta.ToString.Trim
            ElseIf ImporteDesde > 0 Then
                StrinWhere = StrinWhere & " and vta_importe >= " & ImporteDesde.ToString.Trim
            ElseIf ImporteHasta > 0 Then
                StrinWhere = StrinWhere & " and vta_importe <= " & ImporteHasta.ToString.Trim
            End If

            If Cli_Id > 0 Then
                StrinWhere = StrinWhere & " and cli_id = " & Cli_Id.ToString.Trim
            End If

            Cm.CommandText = StringSelect & StrinWhere

            Cm.Connection = Cn
            Dr = Cm.ExecuteReader
            If Dr.HasRows = True Then
                While Dr.Read
                    Dim Venta2 As New clsVentas()
                    Venta2.Vta_Id = Dr.GetInt32(0)
                    Venta2.Vta_Fecha = Dr.GetDateTime(1)
                    If Dr.IsDBNull(2) Then
                        Venta2.Cli_Id = 0
                    Else
                        Venta2.Cli_Id = Dr.GetInt32(2)
                    End If

                    If Dr.IsDBNull(3) Then
                        Venta2.Presu_Id = 0
                    Else
                        Venta2.Presu_Id = Dr.GetInt32(3)
                    End If

                    Venta2.Emp_Id = Dr.GetInt32(4)
                    Venta2.Vta_Importe = Dr.GetDecimal(5)
                    Venta2.Cod_Suc = Dr.GetInt32(6)

                    If (Venta2.Emp_Id > 0) Then
                        Dim Empleado_Dao As New daoEmpleado()
                        Venta2.Empleado = Empleado_Dao.TraeEmpleadoCodSuc(Venta2.Emp_Id, Cod_Suc)
                        Empleado_Dao.Dispose()
                    Else
                        Venta.Empleado = New clsEmpleado()
                    End If

                    If (Venta2.Cli_Id > 0) Then
                        Dim Cliente_Dao As New daoCliente()
                        Venta2.Cliente = Cliente_Dao.TraeClienteCodSuc(Venta2.Cli_Id, Cod_Suc)
                        Cliente_Dao.Dispose()
                    Else
                        Venta2.Cliente = New clsCliente()
                    End If


                    Dim ListaDetalle_Venta As New List(Of clsVentas_Detalle)
                    Dim Detalle_Venta_Dao As New daoVenta_Detalle()
                    ListaDetalle_Venta = Detalle_Venta_Dao.TraeDetallesVentaCodSuc(Venta2.Vta_Id, Cod_Suc)
                    Venta2.Detalle = ListaDetalle_Venta
                    Detalle_Venta_Dao.Dispose()

                    ListaVentas.Add(Venta2)
                    Venta2.Dispose()
                End While

                Dr.Close()
                Cn.Close()
            End If
            Return ListaVentas
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Dispose()
        MyBase.Finalize()
    End Sub

End Class
